package com.punai.customer.controller;

import com.punai.common.annotation.Log;
import com.punai.common.core.controller.BaseController;
import com.punai.common.core.domain.AjaxResult;
import com.punai.common.core.page.TableDataInfo;
import com.punai.common.enums.BusinessType;
import com.punai.common.utils.StringUtils;
import com.punai.customer.domain.CusVersionsDiscrepancy;
import com.punai.customer.domain.contants.CusConstants;
import com.punai.customer.domain.vo.CusDiffLogDataVo;
import com.punai.customer.service.ICusVersionsDiscrepancyService;
import com.punai.standard.service.IStaVersionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 版本信息差异Controller
 * 
 * @author zxw
 * @date 2022-12-13
 */
@RestController
@RequestMapping("/cus/discrepancy")
public class CusVersionsDiscrepancyController extends BaseController
{
    @Autowired
    private ICusVersionsDiscrepancyService cusVersionsDiscrepancyService;
    @Autowired
    private IStaVersionService staVersionService;

    /**
     * 查询版本信息差异列表
     */
    @PreAuthorize("@ss.hasPermi('cus:discrepancy:list')")
    @GetMapping("/list")
    public TableDataInfo list(CusVersionsDiscrepancy staVersionsDiscrepancy)
    {
        startPage();
        List<CusVersionsDiscrepancy> list = cusVersionsDiscrepancyService.selectStaVersionsDiscrepancyList(staVersionsDiscrepancy);
        return getDataTable(list);
    }

    /**
     * 获取版本信息差异详细信息
     */
    @PreAuthorize("@ss.hasPermi('cus:discrepancy:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(cusVersionsDiscrepancyService.selectStaVersionsDiscrepancyById(id));
    }

    /**
     * 新增版本信息差异
     */
    @PreAuthorize("@ss.hasPermi('cus:discrepancy:add')")
    @Log(title = "版本信息差异", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CusVersionsDiscrepancy staVersionsDiscrepancy)
    {
        return toAjax(cusVersionsDiscrepancyService.insertStaVersionsDiscrepancy(staVersionsDiscrepancy));
    }

    /**
     * 修改版本信息差异
     */
    @PreAuthorize("@ss.hasPermi('cus:discrepancy:edit')")
    @Log(title = "版本信息差异", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CusVersionsDiscrepancy staVersionsDiscrepancy)
    {
        return toAjax(cusVersionsDiscrepancyService.updateStaVersionsDiscrepancy(staVersionsDiscrepancy));
    }

    /**
     * 删除版本信息差异
     */
    @PreAuthorize("@ss.hasPermi('cus:discrepancy:remove')")
    @Log(title = "版本信息差异", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cusVersionsDiscrepancyService.deleteStaVersionsDiscrepancyByIds(ids));
    }

    // 公用方法,所以取消权限,只在前台按钮做
//    @PreAuthorize("@ss.hasPermi('cus:discrepancy:getLastPublishId')")
    @GetMapping("/getLastPublishId/{type}")
    public AjaxResult getLastPublishId(@PathVariable String type)
    {
        return AjaxResult.success("success",cusVersionsDiscrepancyService.getLastPublishId(type,getCompanyId()));
    }
    // 同上
//    @PreAuthorize("@ss.hasPermi('cus:discrepancy:publishToSta')")
    @PutMapping("/publishToSta/{type}/{specIds}")
    public AjaxResult publishToSta(@PathVariable String type,@PathVariable String[] specIds) {
        cusVersionsDiscrepancyService.publishToSta(type,specIds,getCompanyId());
        return AjaxResult.success();
    }

    @GetMapping("/recordDifference/{versionId}/{type}/{specIds}")
    public AjaxResult recordDifference(@PathVariable String versionId,@PathVariable String type,@PathVariable String specIds) {
        cusVersionsDiscrepancyService.recordDifference(versionId,type,specIds,getCompanyId());
        return AjaxResult.success();
    }

    @GetMapping("/recordDifferenceMaterial")
    public AjaxResult RecordDifferenceMaterial() {
        String newestId = staVersionService.getNewestId(getCompanyId(),CusConstants.CL_TYPE, null);
        if(StringUtils.isEmpty(newestId)){
            return AjaxResult.success(2001);
        }
        cusVersionsDiscrepancyService.recordDifference(newestId,CusConstants.CL_TYPE,null,getCompanyId());
        return AjaxResult.success(200);
    }

    @GetMapping("/recordDifferenceMechanical")
    public AjaxResult RecordDifferenceMechanical() {
        String newestId = staVersionService.getNewestId(getCompanyId(),CusConstants.JX_TYPE, null);
        if(StringUtils.isEmpty(newestId)){
            return AjaxResult.success(2001);
        }
        cusVersionsDiscrepancyService.recordDifference(newestId,CusConstants.JX_TYPE,null,getCompanyId());
        return AjaxResult.success(200);
    }

    @GetMapping("/recordDifferenceGx/{specId}")
    public AjaxResult recordDifferenceGx(@PathVariable String specId) {
        String newestId = staVersionService.getNewestId(getCompanyId(),CusConstants.GX_TYPE, specId);
        if(StringUtils.isEmpty(newestId)){
            return AjaxResult.success(2001);
        }
        cusVersionsDiscrepancyService.recordDifference(newestId,CusConstants.GX_TYPE,specId,getCompanyId());
        return AjaxResult.success();
    }
    @GetMapping("/recordDifferenceFb/{specId}")
    public AjaxResult recordDifferenceFb(@PathVariable String specId) {
        String newestId = staVersionService.getNewestId(getCompanyId(),CusConstants.FB_TYPE, specId);
        if(StringUtils.isEmpty(newestId)){
            return AjaxResult.success(2001);
        }
        cusVersionsDiscrepancyService.recordDifference(newestId,CusConstants.FB_TYPE,specId,getCompanyId());
        return AjaxResult.success();
    }

    @GetMapping("/getCusDiffLogData")
    public TableDataInfo getCusDiffLogData(CusDiffLogDataVo cusDiffLogDataVo) {
        startPage();
        String type = cusDiffLogDataVo.getType();
        String specIds = cusDiffLogDataVo.getSpecIds();
        String opType = cusDiffLogDataVo.getOpType();

        List<CusVersionsDiscrepancy> assetVersionsDiscrepancies = new ArrayList<>();
        if(StringUtils.equals(type, CusConstants.CL_TYPE) || StringUtils.equals(type, CusConstants.JX_TYPE)){
            assetVersionsDiscrepancies = cusVersionsDiscrepancyService.queryContrast(type,opType,getCompanyId());
        }else {
            assetVersionsDiscrepancies = cusVersionsDiscrepancyService.queryContrast(type,opType,specIds,getCompanyId());
        }
        return getDataTable(assetVersionsDiscrepancies);
    }

}
